package resource

import (
	"github.com/golang/protobuf/proto"
	"git.cloud.inspur.com/ichain/ichain-protos-go/common"
	"git.cloud.inspur.com/ichain/ichain-sdk-go/pkg/providers/ichain"
	"github.com/pkg/errors"
)

const (
	cscc          = "SC_CHAIN"
	csccJoinChain = "JoinChain"
	csccChains    = "GetChains"
)

// createJoinChainInvokeRequest 创建加入链的合约请求
func createJoinChainInvokeRequest(genesisBlock *common.Block) (ichain.ContractInvokeRequest, error) { //nolint

	genesisBlockBytes, err := proto.Marshal(genesisBlock)
	if err != nil {
		return ichain.ContractInvokeRequest{}, errors.Wrap(err, "marshal genesis block failed")
	}

	var args [][]byte
	args = append(args, genesisBlockBytes)

	cir := ichain.ContractInvokeRequest{
		ContractID: cscc,
		Fcn:        csccJoinChain,
		Args:       args,
	}

	return cir, nil
}

// createChainsInvokeRequest 创建查询链的合约请求
func createChainsInvokeRequest() ichain.ContractInvokeRequest {
	cir := ichain.ContractInvokeRequest{
		ContractID: cscc,
		Fcn:        csccChains,
	}
	return cir
}
